package com.mrper.ext.uibase;

import android.app.Activity;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.miguo.framework.request.FailData;
import com.miguo.framework.request.JsonData;
import com.miguo.http.callback.AjaxCallBack;
import com.miguo.ui.R;
import com.mrper.util.ApkUtil;
import com.mrper.util.CommonUtil;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * Created by admin on 2016/3/14.
 * 基础Fragment
 */
public class BasicFragment<B extends ViewDataBinding> extends Fragment implements OnInitContextInterface{

    /** 数据绑定对象 **/
    protected B dataBinder;
    /** 上下文对象 **/
    protected Context context;
    /** activity对象 **/
    protected Activity activity;
    /** 网络回调对象 **/
    protected AjaxCallBack httpCallback;
    //图片加载对象
    protected ImageLoader imageLoader = ImageLoader.getInstance();
    /** 是否执行了第一次加载 **/
    protected boolean isExecFirstLoad = false;
    /** 是否准备了加载数据  **/
    protected boolean isPrepareLoad = false;
    /** 获取statusbar高度  **/
    protected int statusBarHeight = 0;

    @Override
    public void onAttach(Activity context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        dataBinder = DataBindingUtil.inflate(inflater, CommonUtil.inflateContentView(this),null,false);
        initViews();//视图初始化
        return dataBinder.getRoot();
    }

    /** 初始化视图 **/
    protected void initViews(){}

    /** 调用数据初始化加载  **/
    public void initDataLoad(){
        if(!isExecFirstLoad) {
            isPrepareLoad = true;
            if (context != null) {
                isExecFirstLoad = true;
                if (httpCallback == null)
                    initHttpCall();
                doDataLoad();//加载商品数据
            }
        }
    }

    /**  做数据加载  **/
    protected void doDataLoad(){ }

    /** Activity加载完毕事件  **/
    protected void onActivityLoadFinished(){  }

    @Override
    public String getLoadingDialogString(){
        return getString(R.string.common_loading_message);
    }

    /** 是否初始化时显示加载对话框,默认不显示 **/
    @Override
    public boolean isInitShowLoadingDialog(){
        return false;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        this.activity = getActivity();
        statusBarHeight = ApkUtil.getStatusBarHeight(context);
        if(isInitHttpCallback()) initHttpCall();
        onActivityLoadFinished();
        if(isPrepareLoad) initDataLoad();
    }

    /** 初始化网络回调 **/
    protected void initHttpCall(){
        httpCallback = new AjaxCallBack(activity, getLoadingDialogString(), isInitShowLoadingDialog()) {
            @Override
            public void onRequestFinish(JsonData data) {
                super.onRequestFinish(data);
                onLoadHttpDataSuccessful(data);
                hideDialog();
            }

            @Override
            public void onRequestFail(FailData data) {
                super.onRequestFail(data);
                onLoadHttpDataFaield(data);
                hideDialog();
            }
        };
    }

    /** 是否立即初始化Http回调,默认为true **/
    protected boolean isInitHttpCallback(){
        return true;
    }

    @Override
    public void onLoadHttpDataSuccessful(JsonData data) {

    }

    @Override
    public void onLoadHttpDataFaield(FailData data) {

    }
}
